package com.ktwlsoft.archivemanage.controller;

import com.ktwlsoft.archivemanage.domain.business.businessService.MendListService;
import com.ktwlsoft.archivemanage.domain.business.businessService.NotifyMessageScheduleTask;
import com.ktwlsoft.archivemanage.domain.business.businessService.NotifySetService;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.OperationRepositoryService;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.mend.MendListRepositoryService;
import com.ktwlsoft.archivemanage.domain.dao.entity.OperationLog;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.mendList.MendListModifyRequest;
import com.ktwlsoft.archivemanage.payload.mendList.MendListPageRequest;
import com.ktwlsoft.archivemanage.payload.mendList.MendVerifyRequest;
import com.ktwlsoft.archivemanage.payload.mendList.NotifySetRequest;
import com.ktwlsoft.archivemanage.payload.operationLog.SaveLogRequest;
import com.ktwlsoft.archivemanage.service.security.UserAuthenticate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(value = "自查复查整改单",description = "自查复查整改单",tags = "1.2")
@RestController
@RequestMapping("/selfOrRepeatMendlist")
public class SelfOrRepeatMendListController {

    private final MendListRepositoryService mendListRepositoryService;

    private final MendListService mendListService;

    private final NotifyMessageScheduleTask notifyMessageScheduleTask;

    private final NotifySetService notifySetService;

    public SelfOrRepeatMendListController(MendListRepositoryService mendListRepositoryService, MendListService mendListService, NotifyMessageScheduleTask notifyMessageScheduleTask, NotifySetService notifySetService) {
        this.mendListRepositoryService = mendListRepositoryService;
        this.mendListService = mendListService;
        this.notifyMessageScheduleTask = notifyMessageScheduleTask;
        this.notifySetService = notifySetService;
    }

    @GetMapping("/findByPagePc")
    @ApiOperation(value = "PC分页检索")
    @UserAuthenticate(actionCode = "MEND_LIST", skip = true)
    public ResponseEntity<?> findByPagePC(MendListPageRequest request) {
        request.setInspectType("self_or_repeat");
        return ResponseEntity.ok(Result.ok(mendListRepositoryService.findMendListPage(request,"pc")));
    }

    @GetMapping("/findByPageMini")
    @ApiOperation(value = "小程序分页检索")
    public ResponseEntity<?> findByPageMini(MendListPageRequest request) {
        request.setInspectType("self_or_repeat");
        return ResponseEntity.ok(Result.ok(mendListRepositoryService.findMendListPage(request,"wx")));
    }

    @GetMapping("/findDetailPc")
    @ApiOperation("PC查询整改单详情")
    @UserAuthenticate(actionCode = "MEND_DETAIL", skip = true)
    public ResponseEntity<?> findDetailPc(@ApiParam(value = "整改单Id") @RequestParam Long id) {
        return ResponseEntity.ok(Result.ok(mendListRepositoryService.findDetail(id)));
    }

    @GetMapping("/findDetailMini")
    @ApiOperation("小程序查询整改单详情")
    public ResponseEntity<?> findDetailMini(@RequestParam Long id) {
        return ResponseEntity.ok(Result.ok(mendListRepositoryService.findDetail(id)));
    }

    @PostMapping("/modifyMendListMini")
    @ApiOperation("小程序修改整改单")
    public ResponseEntity<?> modifyMendListMini(@RequestBody MendListModifyRequest request) {
        mendListService.modifyMendList(request);
        return ResponseEntity.ok(Result.ok("修改完成"));
    }

    @PostMapping("/verifyMend")
    @ApiOperation("PC端验证整改")
    @UserAuthenticate(actionCode = "MEND_VERIFY")
    public ResponseEntity<?> verifyMend(@RequestBody MendVerifyRequest request) {
        mendListService.verifyMend(request);
        String ifPass = request.getPass() ? "通过" : "不通过";
        OperationRepositoryService.saveLog(new SaveLogRequest(
                OperationLog.OperationType.VERIFY,
                request.getArchiveUnit() + "检查整改（" + request.getGuidCode() + ")验证" + ifPass,
                "检查整改",
                OperationLog.BusinessType.BUSINESS
        ));
        notifyMessageScheduleTask.mendNotPassNotifyMessage(request.getMendId(),request.getPass());
        return ResponseEntity.ok(Result.ok("验证完成"));
    }

    @DeleteMapping("/mendDelete/{mendId}")
    @ApiOperation("删除整改单")
    @UserAuthenticate(actionCode = "MEND_DELETE")
    public ResponseEntity<?> deleteMend(@PathVariable Long mendId) {
        mendListService.deleteMend(mendId);
        return ResponseEntity.ok(Result.ok("删除完成"));
    }

    @ApiOperation("消息通知设置")
    @PutMapping("/setNotify")
    @UserAuthenticate(actionCode = "MEND_NOTIFY_SET")
    public ResponseEntity<?> setNotify(@RequestBody @Valid NotifySetRequest request) {
        mendListService.setNotifySet(request);
        OperationRepositoryService.saveLog(new SaveLogRequest(
                OperationLog.OperationType.SET,
                "更改了检查整改设置",
                "检查整改",
                OperationLog.BusinessType.BUSINESS
        ));
        return ResponseEntity.ok(Result.ok("设置成功"));
    }

    @ApiModelProperty("获取通知设置")
    @GetMapping("/getNotifySet")
    @UserAuthenticate(actionCode = "MEND_NOTIFY_GET", skip = true)
    public ResponseEntity<?> getNotifySet() {
        return ResponseEntity.ok(Result.ok(notifySetService.getNotifySet("MEND_WARNING")));
    }

    @GetMapping("/findLogByPage")
    @ApiOperation("整改单日志")
    @UserAuthenticate(actionCode = "MEND_HISTORY", skip = true)
    public ResponseEntity<?> findLogByPage(@RequestParam Integer pn,@RequestParam Integer pageSize,@RequestParam Long mendId) {
        return ResponseEntity.ok(Result.ok(mendListService.findLogByPage(pn, pageSize, mendId)));
    }

    /**
     * @Description 获取小程序整改单所有指导人
     * @dateTime 2019-12-04 17:21
     * @author wangchong
    */
    @GetMapping("/wxGetInstructors")
    @ApiOperation("获取小程序整改单所有指导人")
    public ResponseEntity<?> wxGetInstructors() {
        return ResponseEntity.ok(Result.ok(mendListService.wxGetInstructors()));
    }
}
